package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.ProductMaterial;
import org.linlinjava.litemall.db.domain.ProductMaterialExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductMaterialService {


    @Resource
    private MaterialMapper materialMapper;
    @Resource
    private FormulaMapper formulaMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private FormulaMaterialMapper formulaMaterialMapper;
    @Resource
    private ProductMaterialMapper productMaterialMapper;

    public int updateById(ProductMaterial brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return productMaterialMapper.updateByPrimaryKeySelective(brand);
    }

    public void deleteById(Integer id) {
        productMaterialMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(ProductMaterial brand) {
        LocalDateTime now = LocalDateTime.now();
        brand.setAddTime(now);
        brand.setUpdateTime(now);
        productMaterialMapper.insertSelective(brand);
    }

    public List<ProductMaterial> all() {
        ProductMaterialExample example = new ProductMaterialExample();
        example.or().andDeletedEqualTo(false);
        return productMaterialMapper.selectByExample(example);
    }

    public List<ProductMaterial> query(Integer page, Integer limit, String sort, String order) {
        ProductMaterialExample example = new ProductMaterialExample();
        example.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return productMaterialMapper.selectByExample(example);
    }

    public List<ProductMaterial> query(Integer page, Integer limit) {
        return query(page, limit, "add_time", "desc");
    }

    public ProductMaterial findById(Integer id) {
        return productMaterialMapper.selectByPrimaryKey(id);
    }

    public List<ProductMaterial> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
        ProductMaterialExample example = new ProductMaterialExample();
        ProductMaterialExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
//        if (!StringUtils.isEmpty(name)) {
//            criteria.andNameLike("%" + name + "%");
//        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return productMaterialMapper.selectByExample(example);
    }
}
